package com.wc.算法提高课.C第三章_图论.单源最短路的建图方式.最优乘车;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/10 13:18
 * @description https://www.acwing.com/problem/content/922/
 */
public class Main {
    /**
     * 思路：<p>
     * 可以建立每个公交线前面的站到后面的所有站建立图 1
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 510, INF = 0x3f3f3f3f;
    static boolean[][] g = new boolean[N][N];
    static int[] dist = new int[N], q = new int[N], stop = new int[N];
    static boolean[] st = new boolean[N];
    static int n, m;

    public static void main(String[] args) {
        m = sc.nextInt();
        n = sc.nextInt();
        while (m-- > 0) {
            String[] ss = sc.nextLine().split(" ");
            int cnt = ss.length;
            for (int i = 0; i < cnt; i++) stop[i] = Integer.parseInt(ss[i]);
            for (int i = 0; i < cnt; i++) {
                for (int j = i + 1; j < cnt; j++) {
                    int a = stop[i], b = stop[j];
                    g[a][b] = true;
                }
            }
        }
        bfs();
        if (dist[n] == INF) out.println("NO");
        else out.println(Math.max(dist[n] - 1, 0));
        out.flush();
    }

    static void bfs() {
        Arrays.fill(dist, 1, n + 1, INF);
        int hh = 0, tt = 1;
        q[0] = 1;
        st[1] = true;
        dist[1] = 0;
        while (hh <= tt) {
            int t = q[hh++];
            st[t] = false;
            for (int i = 1; i <= n; i++) {
                if (g[t][i] && dist[i] > dist[t] + 1) {
                    dist[i] = dist[t] + 1;
                    if (!st[i]) {
                        q[tt++] = i;
                        st[i] = true;
                    }
                }
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
